#https://leetcode.cn/problems/fruit-into-baskets/solutions/1706686/by-0xula58rcn-umun/

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int ans = 0;
            int i=0;
        unordered_map<int, int> cnt;
        for (int j = 0; j < fruits.size(); j++) {
            cnt[fruits[j]]++;
            while (cnt.size() > 2) {
            if (cnt[fruits[i]] == 1) {
                cnt.erase(fruits[i]);
            } else {
                cnt[fruits[i]]--;
            }
            i++;
        }
            ans = max(ans, j - i+1 );
        }
        return ans;
    }

};